home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacTCPToolBx / Source Code ƒ / TCPActiveOpen.p < prev    next >
Text File  |  1989-06-01  |  3KB  |  117 lines

  1. (*
  2.     TCPActiveOpen(remoteIP,remotePort,localPort) -- Open a TCP connection to the socket specified, and
  3.         return a connection ID.
  4.  
  5.     To compile and link this file using Macintosh Programmer's Workshop,
  6.  
  7.         pascal -w TCPActiveOpen.p
  8.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=7860 -sn Main=TCPActiveOpen ∂
  9.             TCPActiveOpen.p.o "{Libraries}HyperXLib.o" "{MPW}"Libraries:interface.o
  10.  
  11.     © Copyright 1988 by Apple Computer, Inc.
  12.  
  13.     Initial coding 12/88 by Harry R. Chesley.
  14. *)
  15.  
  16. {$R-}
  17.  
  18. {$S TCPActiveOpen }     { Segment name must be the same as the command name. }
  19.  
  20. unit DummyUnit;
  21.  
  22. interface
  23.  
  24. uses MemTypes, QuickDraw, OSIntf, HyperXCmd;
  25.  
  26. procedure EntryPoint(paramPtr: XCmdPtr);
  27.     
  28. implementation
  29.  
  30. procedure TCPActiveOpen(paramPtr: XCmdPtr); forward;
  31.  
  32. procedure EntryPoint(paramPtr: XCmdPtr);
  33.  
  34.     begin
  35.         TCPActiveOpen(paramPtr);
  36.     end;
  37.  
  38. procedure TCPActiveOpen(paramPtr: XCmdPtr);
  39.  
  40.     var remoteIP: longInt;        { IP address of desintation. }
  41.         remotePort: integer;    { Port address of destination. }
  42.         localPort: integer;        { Port address on our side. }
  43.         dummy: integer;
  44.         resultStr: str255;
  45.  
  46.     procedure Fail(errMsg: Str255); { set theResult and quit }
  47.         begin
  48.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  49.             exit(TCPActiveOpen);
  50.         end;
  51.  
  52.     {$I TCPUtil.inc}
  53.  
  54.     procedure FailAndDispose(errMsg: Str255);
  55.         begin
  56.             DisposPtr(Ptr(Connection));
  57.             Fail(errMsg)
  58.         end;
  59.  
  60.     procedure createStream;
  61.         { Make a new stream. }
  62.  
  63.     begin
  64.         { Allocate our connection block, and fill it in. }
  65.         Connection := TCPConnectionPtr(NewPtr(sizeof(TCPConnectionType)));
  66.         if Connection = nil then Fail('§§§ buffer allocation failed §§§');
  67.         Connection^.magic := MAGICNUMBER;
  68.         Connection^.incomingSize := 0;
  69.         { Open the driver. }
  70.         if OpenDriver('.IPP',SyncControlBlock.ioCRefNum) <> noErr then
  71.             FailAndDispose('§§§ driver open failed §§§');
  72.         { Create a stream. }
  73.         ZeroIOParms;
  74.         PutControlLongAtOffset(ord4(@Connection^.buffer),32);
  75.         PutControlLongAtOffset(TCPBUFFERSIZE,36);
  76.         SyncControlBlock.csCode := TCPcsCreate;
  77.         if PBControl(@SyncControlBlock,false) <> noErr then
  78.             FailAndDispose('§§§ stream creation failed §§§');
  79.         Connection^.asyncControlBlock := SyncControlBlock;
  80.     end;
  81.  
  82.     begin
  83.         if (paramPtr^.paramCount <> 2) and (paramPtr^.paramCount <> 3) then
  84.             Fail('§§§ parameter count is not 2 or 3 §§§');
  85.  
  86.         remoteIP := GetLongParm(1);            { First parameter is the remote IP address. }
  87.         remotePort := GetLongParm(2);        { Second parameter is the remote port number. }
  88.         if paramPtr^.paramCount = 3 then localPort := GetLongParm(3)    {Third parameter is the local port #. }
  89.         else localPort := 0;                            { ... which is optional. }
  90.  
  91.         { Make a new stream. }
  92.         createStream;
  93.  
  94.         { Issue an active open. }
  95.         ZeroIOParms;
  96.         PutControlLongAtOffset(remoteIP,36);
  97.         PutControlWordAtOffset(remotePort,40);
  98.         PutControlWordAtOffset(localPort,46);
  99.         SyncControlBlock.csCode := TCPcsActiveOpen;
  100.         with Connection^ do
  101.             begin
  102.                 asyncControlBlock := SyncControlBlock;
  103.                 if PBControl(@asyncControlBlock,true) <> noErr then
  104.                     begin
  105.                         ZeroIOParms;
  106.                         SyncControlBlock.csCode := TCPcsRelease;
  107.                         dummy := PBControl(@SyncControlBlock,false);
  108.                         FailAndDispose('§§§ connection open failed §§§');
  109.                     end;
  110.             end;
  111.  
  112.         LongToStr(paramPtr,Ord4(Connection),resultStr);
  113.         paramPtr^.returnValue := PasToZero(paramPtr,resultStr)
  114.     end;
  115.  
  116. end.
  117.